package com.shrct.businessprocess.budget.impl;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.shrct.businessprocess.budget.BudgetType;
import com.shrct.businessprocess.budget.BudgetTypeManager;
import com.shrct.businessprocess.budget.BudgetTypeProvider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "BudgetTypeManager")
@SuppressWarnings("all")
public class BudgetTypeManagerImpl implements BudgetTypeManager {
  @Autowired
  private List<BudgetTypeProvider> budgetTypeProviders = new Function0<List<BudgetTypeProvider>>() {
    public List<BudgetTypeProvider> apply() {
      ArrayList<BudgetTypeProvider> _newArrayList = CollectionLiterals.<BudgetTypeProvider>newArrayList();
      return _newArrayList;
    }
  }.apply();
  
  public List<BudgetType> getAllBudgetTypes() {
    final Function1<BudgetTypeProvider,Collection<BudgetType>> _function = new Function1<BudgetTypeProvider,Collection<BudgetType>>() {
        public Collection<BudgetType> apply(final BudgetTypeProvider it) {
          Collection<BudgetType> _registeredBudgetTypes = it.getRegisteredBudgetTypes();
          return _registeredBudgetTypes;
        }
      };
    List<Collection<BudgetType>> _map = ListExtensions.<BudgetTypeProvider, Collection<BudgetType>>map(this.budgetTypeProviders, _function);
    Iterable<BudgetType> _flatten = Iterables.<BudgetType>concat(_map);
    List<BudgetType> _list = IterableExtensions.<BudgetType>toList(_flatten);
    return _list;
  }
  
  public BudgetType getBudgetType(final String typeid) {
    List<BudgetType> _allBudgetTypes = this.getAllBudgetTypes();
    final Function1<BudgetType,Boolean> _function = new Function1<BudgetType,Boolean>() {
        public Boolean apply(final BudgetType it) {
          String _typeid = it.getTypeid();
          boolean _equals = Objects.equal(_typeid, typeid);
          return Boolean.valueOf(_equals);
        }
      };
    Iterable<BudgetType> _filter = IterableExtensions.<BudgetType>filter(_allBudgetTypes, _function);
    final Function1<BudgetType,Boolean> _function_1 = new Function1<BudgetType,Boolean>() {
        public Boolean apply(final BudgetType it) {
          return Boolean.valueOf(true);
        }
      };
    BudgetType _findFirst = IterableExtensions.<BudgetType>findFirst(_filter, _function_1);
    return _findFirst;
  }
}
